/*
 * @Description:
 * @Version: 2.0
 * @Autor: wiz
 * @Date: 2023-03-20 14:24:30
 * @LastEditors: wiz
 * @LastEditTime: 2023-12-18 09:36:31
 */
import * as ifs from '@/obj/ifs';
import Vue from 'vue';
import store from '../../store';
import wsStore from '../../store/modules/ws';

const getToken: any = () => store.getters.Token;
const connect = (webSocketHost: string): any => {
  const token: any = getToken();
  Vue.prototype.$connect(`${webSocketHost}?token=${token}`, {
    store: wsStore,
    reconnection: true,
    reconnectionAttempts: 5,
    reconnectionDelay: 3000
  });
};

const send = (
  outDataType: string,
  outData: string | object | number,
  timeout = 20000
): Promise<any> => {
  return wsStore.dispatch('sendMessage', {
    outData,
    outDataType,
    timeout
  });
};

const sendMsg = (msgParams: ifs.I_sendWsMsgParams): Promise<any> => {
  return wsStore.dispatch('sendMessage', msgParams);
};

const disconnect = (): any => {
  Vue.prototype.$disconnect();
};

export default {
  connect,
  disconnect,
  /**
   * @description 此方法废弃，请用senMsg
   */
  send,
  sendMsg
};
